package com.Product;
import com.enclose.dataBasePool;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.model.infromatin.informationResult;
import com.model.infromatin.informationResultSon;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

@WebServlet("/Product/information")
public class information extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //1.获取参数
        Integer categoryId = 0;
        if(req.getParameter("categoryId") != null){
            categoryId = Integer.valueOf(req.getParameter("categoryId"));
        }
        Integer pageNum = 0;
        if(req.getParameter("pageNum") != null){
            pageNum = Integer.valueOf(req.getParameter("pageNum"));
        }
        Integer pageSize = 0;
        if(req.getParameter("pageSize") != null){
            pageSize = Integer.valueOf(req.getParameter("categoryId"));
        }


        List<informationResultSon> son = new ArrayList<>();
        Integer Total = null;
        //创建存储对象

        //访问数据库(fenzhu)
        Statement statement,statement1 =null;
        ResultSet resultSet,resultSet1 = null;
        try {

//            JDBC jdbc = new JDBC();
            dataBasePool dataBasePool = new dataBasePool();
            String sql ="SELECT p.product_id,p.product_name,p.category_id,c.category_name,p.product_title,p.product_intro,p.product_picture,p.product_price,p.product_selling_price\n" +
                    "FROM category c JOIN product p ON c.category_id =p.category_id WHERE c.category_id = "+categoryId +" LIMIT "+pageNum+","+pageSize;

//            SELECT p.product_id,p.product_name,p.category_id,c.category_name,p.product_title,p.product_intro,p.product_picture,p.product_price,p.product_selling_price
//            FROM category c
//            JOIN product p
//            ON c.category_id =p.category_id
//            WHERE c.category_id = 1
//            LIMIT 0,1

             statement = dataBasePool.getStatement();
             resultSet = statement.executeQuery(sql);


            if (resultSet.next()){
                son.add(new informationResultSon(
                        resultSet.getInt("product_id"),
                        resultSet.getString("product_name"),
                        resultSet.getInt("category_id"),
                        resultSet.getString("category_name"),
                        resultSet.getString("product_title"),
                        resultSet.getString("product_intro"),
                        resultSet.getString("product_picture"),
                        resultSet.getString("product_price"),
                        resultSet.getString("product_selling_price")
                ));


            }

            //获取参数的总条数
            String sql2 = "SELECT COUNT(*)\n" +
                    "FROM category c\n" +
                    "JOIN product p\n" +
                    "ON c.category_id =p.category_id";

             statement1 = dataBasePool.getStatement();

             resultSet1 = statement1.executeQuery(sql2);

            while (resultSet1.next()){
                Total = resultSet1.getInt(1);
            }
            dataBasePool.close(statement,resultSet);
            dataBasePool.close(statement1,resultSet1);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }

        //将数据库中的数据导入结果对象中
        informationResult informationResult =new informationResult();
        informationResult.setRows(son);
        informationResult.setCode("200");
        informationResult.setMsg("success!");
        informationResult.setTotal(Total);
        //将数据输出
        ObjectMapper objectMapper = new ObjectMapper();
        String str = objectMapper.writeValueAsString(informationResult);
        resp.getWriter().write(str);

    }

//    public static void main(String[] args) {
//        try {
//            ComboPooledDataSource comboPooledDataSource = new ComboPooledDataSource();
//            Connection connection1  = comboPooledDataSource.getConnection();
//            Connection connection2 = comboPooledDataSource.getConnection();
//            System.out.println(connection2);
//            System.out.println(connection1);
//            connection1.close();
//            connection2.close();
//        } catch (SQLException e) {
//            throw new RuntimeException(e);
//        }
//    }
}
